const express = require("express");
const router = express.Router();

const moment = require("moment"); // 日期处理类库
const AccountModel = require("../mongodb/models/Account");
const { CODE, MESSAGE } = require("../public/constant");

router.get("/", (req, res) => {
    res.redirect("/accounts");
}); // 根目录重定向至列表页
router.get("/accounts", (req, res) => {
    AccountModel.find().sort({ date: "desc" }).exec((error, data) => {
        if (error) {
            console.log(error);
            const code = CODE.SOURCE_ERROR;
            const message = MESSAGE.SOURCE_ERROR;
            res.json({ code, message }); return;
        }
        console.log(data);
        const code = CODE.SUCCESS;
        const message = MESSAGE.SUCCESS;
        res.json({ code, message, data });
    }); // 根据日期倒序排列，最新记录在最前
}); // 记账簿列表获取操作
router.post("/account", (req, res) => {
    const { date, type, money } = req.body;
    AccountModel.create({
        ...req.body,
        date: moment(date).toDate(), //  转换成日期格式
        type: Number(type),  // 转换成数字类型(标记)
        money: Number(money) // 转换成数字类型(金额)
    }, error => {
        if (error) {
            console.log(error);
            const code = CODE.CREATE_ERROR;
            const message = MESSAGE.CREATE_ERROR;
            res.json({ code, message }); return;
        }
        const code = CODE.SUCCESS;
        const message = MESSAGE.SUCCESS;
        res.json({ code, message });
    });
}); // 记账簿项目创建操作
router.get("/account/:id", (req, res) => {
    AccountModel.findById(req.params.id, (error, data) => {
        if (error) {
            console.log(error);
            const code = CODE.DETAIL_ERROR;
            const message = MESSAGE.DETAIL_ERROR;
            res.json({ code, message }); return;
        }
        console.log(data);
        const code = CODE.SUCCESS;
        const message = MESSAGE.SUCCESS;
        res.json({ code, message, data });
    });
}); // 记账簿项目获取操作
router.patch("/account/:id", (req, res) => {
    const { date, type, money } = req.body;
    AccountModel.updateOne({
        _id: req.params.id
    }, {
        ...req.body,
        date: moment(date).toDate(), //  转换成日期格式
        type: Number(type),  // 转换成数字类型(标记)
        money: Number(money) // 转换成数字类型(金额)
    }, error => {
        if (error) {
            console.log(error);
            const code = CODE.MODIFY_ERROR;
            const message = MESSAGE.MODIFY_ERROR;
            res.json({ code, message }); return;
        }
        const code = CODE.SUCCESS;
        const message = MESSAGE.SUCCESS;
        res.json({ code, message });
    });
}); // 记账簿项目修改操作
router.delete("/account/:id", (req, res) => {
    AccountModel.deleteOne({
        _id: req.params.id
    }, error => {
        if (error) {
            console.log(error);
            const code = CODE.DELETE_ERROR;
            const message = MESSAGE.DELETE_ERROR;
            res.json({ code, message }); return;
        }
        const code = CODE.SUCCESS;
        const message = MESSAGE.SUCCESS;
        res.json({ code, message });
    });
}); // 记账簿项目删除操作
module.exports = router;
